package com.mall.gateway.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * @author houyong
 * @ProjectName V-Mall
 * @Description:
 * @date 2019/04/23 下午10:15
 */
@Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider {
    //出版本进行固定的服务配置
//    @Value("${rest.api.names}")
//    private String[] apiNames;

    @Autowired
    private RouteLocator routeLocator;


    @Override
    public List<SwaggerResource> get() {
        List resources = new ArrayList<>();
        resources.add(swaggerResource("mall-gateway", "/v2/api-docs", "2.0"));
        //routeLocator.getRoutes() 获取的内容是配置文件中zuul.routes下的路由配置

        routeLocator.getRoutes().forEach(route -> {
            if (!"mall-gateway".equals(route.getId())) {
                resources.add(swaggerResource(route.getId(), "/" + route.getId() + "/v2/api-docs", "2.0"));
            }
        });
        return resources;
    }

    private SwaggerResource swaggerResource(String name, String location, String version) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setLocation(location);
        swaggerResource.setSwaggerVersion(version);
        return swaggerResource;
    }
}
